﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using System.Collections.Generic;

namespace AAEngine.Engine.Core
{
    public class EventDispatcher : IEventDispatcher
    {
        private Dictionary<string, List<Action<Event>>> _listeners = new Dictionary<string, List<Action<Event>>>();

        public void AddEventListener(string type, Action<Event> callback)
//        public void AddEventListener(string type, Action<Event> callback, int priority = 0, bool useWeakReference = false)
        {
            if (!_listeners.ContainsKey(type))
            {
                _listeners[type] = new List<Action<Event>>();
            }

            var list = _listeners[type];
            list.Add(callback);
        }

        public void RemoveEventListener(string type, Action<Event> callback)
        {
            if (!_listeners.ContainsKey(type))
            {
                return;
            }

            var list = _listeners[type];
            list.Remove(callback);
        }

        public void DispatchEvent(Event e)
        {
            if (!_listeners.ContainsKey(e.Type))
            {
                return;
            }

            var list = _listeners[e.Type];

            foreach (var listener in list)
            {
                listener(e);
            }
        }

        public bool HasEventListeners(string type)
        {
            if (!_listeners.ContainsKey(type))
            {
                return false;
            }

            var list = _listeners[type];

            return list.Count > 0;
        }
    }
}
